package com.adinnet.common.outbound.pay.wxpay;

import com.adinnet.common.utils.PropertiesConfig;
import com.adinnet.common.outbound.pay.utils.AlipaySignature;
import com.adinnet.common.outbound.pay.utils.MD5Util;

import java.io.*;
import java.util.Map;
/**
 * 微信双向证书配置
 */
public class MyConfig extends WXPayConfig {

    private byte[] certData;

    public MyConfig() throws Exception {
        String certPath = PropertiesConfig.weiXinCert;
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public String getAppID() {
        return PropertiesConfig.weiXinSmallProgramId;
    }

    public String getMchID() {
        return PropertiesConfig.weiXinMchId;
    }

    public String getKey() {
        return PropertiesConfig.weiXinPartnerKey;
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    @Override
    IWXPayDomain getWXPayDomain() {
        IWXPayDomain iwxPayDomain = new IWXPayDomain() {

            public void report(String domain, long elapsedTimeMillis, Exception ex) {

            }

            public DomainInfo getDomain(WXPayConfig config) {
                return new IWXPayDomain.DomainInfo("api.mch.weixin.qq.com", true);
            }
        };
        return iwxPayDomain;
    }

    public static String createSign(String characterEncoding, Map<String,String> parameters , String partnerKye) {
        String content = null;
        try {
            content = AlipaySignature.getSignCheckContentV(parameters ,Boolean.FALSE).get("content");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        content += "&key=" +  partnerKye;
        String sign = MD5Util.MD5Encode(content, characterEncoding).toUpperCase();
        return sign;
    }
}